<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/*
 * CURL获取JSON数据
 * */
function curl_api($url,$curlPost){
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    //设置是通过post还是get方法
    curl_setopt($ch,CURLOPT_POST,1);
    //传递的变量
    curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);
    $data = curl_exec($ch);
    curl_close($ch);
    $data=json_decode($data);
    return $data;
}

/*
 * 获取客户端IP地址
 * */
function getIP(){
    global $ip;
    if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else $ip = "Unknow IP";
    return $ip;
}

/*
 * Hook触发函数
 * */
function tag($tag, $params=NULL) {
    \Think\Hook::listen($tag,$params);
}

/*
 * 操作成功
 * */
function op_success(){
    $result=[
        'code'=>1,
        'text'=>'操作成功'
    ];
    return $result;
}

/*
 * 操作失败
 * */
function op_fail(){
    $result=[
        'code'=>0,
        'text'=>'操作失败'
    ];
    return $result;
}

/*
 * 超级管理员
 * */
function op_superadmin(){
    $result=[
        'code'=>0,
        'text'=>'模因警告'
    ];
    return $result;
}

/*
 * 权限验证失败
 * */
function op_noper(){
    $result=[
        'code'=>0,
        'text'=>'没有权限'
    ];
    return $result;
}

/*
    * get权限验证
    * */
function checkAuthority($controller="",$actionName=""){
    if (isset($_SESSION["authority"])){
        if (isset($_SESSION["authority"][$controller])){
            $authority=$_SESSION["authority"][$controller];
            if(in_array($actionName,$authority)){
                return true;
            }
        }
    }
    return false;
}

/*
 * layer 定制错误框
 * */
function validate_error($msg=''){
    $result=
        <<<EOF
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>账户数据</title>
    <link href="/static/plugin/layer/theme/default/layer.css">
    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <script type="text/javascript" src="/static/plugin/layer/layer.js"></script>
</head>
<body>
    <script>
      layer.msg('$msg',{time:1000});
    </script>
</body>

</html>
EOF;
    return $result;
}

/*
 * echo 无权限页面
 * */
function forbidden($css_js){
    $result=
        <<<EOF
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>NO PERM*******</title>
    {$css_js}
    <style>
        body{
            background-color: #f5f5f5;
        }
    </style>
</head>
<body >
<div class="yui_height well">
    <h1 class="grey lighter smaller">
        <span class="blue bigger-125">
            <i class="icon-random"></i>
            500
        </span>
        没有权限
    </h1>

    <hr>
    <h3 class="lighter smaller">
        高度机密
        <br>
        严禁未经授权的人员进行访问
        <br>
        肇事者将被监控，定位并处理
    </h3>

    <div class="space"></div>
    <div>
        <h4 class="lighter smaller">警告：</h4>

        <ul class="list-unstyled spaced inline bigger-110 margin-15">
            <li>
                <i class="icon-hand-right blue"></i>
                你已经被警告过了。
            </li>

            <li>
                <i class="icon-hand-right blue"></i>
                任何未经授权之人员访问该文档将立即被BERRYMAN-LANGFORD模因抹杀触媒处决。在未接种合适模因疫苗的情況下向下滚动页面将立刻导致心脏骤停死亡。
            </li>
        </ul>
    </div>

</div>
</body>
</html>
EOF;
    return $result;
}

/*
 * js css 引入
 * */
function js_css(){
    $result=
        <<<EOF
<link rel="shortcut icon" href="/static/images/ico.jpg" media="screen" />

<link href="/static/plugin/ace/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="/static/plugin/ace/css/font-awesome.min.css" />

<!--[if IE 7]>
<link rel="stylesheet" href="/static/plugin/ace//css/font-awesome-ie7.min.css" />
<![endif]-->
<link rel="stylesheet" href="/static/plugin/ace/font/fontawesome-webfont.woff" />

<!-- ace styles -->

<link rel="stylesheet" href="/static/plugin/ace/css/ace.min.css" />
<link rel="stylesheet" href="/static/plugin/ace/css/ace-rtl.min.css" />
<link rel="stylesheet" href="/static/plugin/ace/css/ace-skins.min.css" />

<!--[if lte IE 8]>
<link rel="stylesheet" href="/static/plugin/ace/css/ace-ie.min.css" />
<![endif]-->
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<!-- inline styles related to this page -->
<link href="/static/plugin/layer/theme/default/layer.css">
<script type="text/javascript" src="/static/plugin/layer/layer.js"></script>
<!-- ace settings handler -->


<script src="/static/plugin/ace/js/ace-extra.min.js"></script>

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->

<!--[if lt IE 9]>
<script src="/static/plugin/ace/js/html5shiv.js"></script>
<script src="/static/plugin/ace/js/respond.min.js"></script>
<![endif]-->


<script src="/static/plugin/ace/js/bootstrap.min.js"></script>
<script src="/static/plugin/ace/js/typeahead-bs2.min.js"></script>

<!-- page specific plugin scripts -->

<!--[if lte IE 8]>
<script src="/static/plugin/ace/js/excanvas.min.js"></script>
<![endif]-->

<script src="/static/plugin/ace/js/jquery-ui-1.10.3.custom.min.js"></script>
<script src="/static/plugin/ace/js/jquery.ui.touch-punch.min.js"></script>
<script src="/static/plugin/ace/js/jquery.slimscroll.min.js"></script>
<script src="/static/plugin/ace/js/jquery.easy-pie-chart.min.js"></script>
<script src="/static/plugin/ace/js/jquery.sparkline.min.js"></script>
<script src="/static/plugin/ace/js/flot/jquery.flot.min.js"></script>
<script src="/static/plugin/ace/js/flot/jquery.flot.pie.min.js"></script>
<script src="/static/plugin/ace/js/flot/jquery.flot.resize.min.js"></script>

<script src="/static/plugin/ace/js/ace-elements.min.js"></script>
<script src="/static/plugin/ace/js/ace.min.js"></script>
EOF;
    return $result;
}